//## 그룹 콤보 Refresh ####################
function fnSetComCdGroup()
{
	var valUrl = "/action/comCode/listComCodeGroup.asp";
	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		//data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			var comCdCombo = 	"<option value=\"\">--그룹--</option>";

			$(xml).find("ComCdGROUP").each(function(idx){
				comCdCombo +=	"<option value=\""+$(this).find("groupCd").text()+"\">"+$(this).find("groupNm").text()+"</option>";
			});
			$("#findComCdGroupCombo").html(comCdCombo);
			$("#groupCd").html(comCdCombo);
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 기초 코드  정보 저장 ####################
function fnSave()
{
	if (!$("#groupCd").val())
	{
		alert("그룹을 바르게 선택해 주세요!");
		$("#groupCd").focus();
		return;
	}
	if (!$("#itemCd").val())
	{
		alert("아이템 코드를 바르게 입력해 주세요!");
		$("#itemCd").focus();
		return;
	}
	if (!$("#itemNm").val())
	{
		alert("아이템 이름을 바르게 입력해 주세요!");
		$("#itemNm").focus();
		return;
	}
	var valUrl  = '/action/comCode/save.asp';
	var param  = "groupCd="+$("#groupCd").val();
		param += "&itemCd="+$("#itemCd").val();
		param += "&itemNm="+encodeURIComponent($("#itemNm").val());
		param += "&groupNm="+encodeURIComponent($('#groupCd :selected').text());
		param += "&itemSeq="+$("#itemSeq").val();
		param += "&flag="+$("#flag").val();
	
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 기초 코드  정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").css("display","");
	setNullRow("loadSpan","1");
	var valUrl = "/action/comCode/list.asp";
	var param = "findGroupCd="+encodeURIComponent($("#findComCdGroupCombo").val());
		param += "&findItem="+encodeURIComponent($("#findItem").val());

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("ComCodes").find("ComCode").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var groupCd		= $(G_grid01Rslt).find("ComCodes").find("ComCode").eq(j).find("groupCd").text();
		var groupNm		= $(G_grid01Rslt).find("ComCodes").find("ComCode").eq(j).find("groupNm").text();
		var itemCd		= $(G_grid01Rslt).find("ComCodes").find("ComCode").eq(j).find("itemCd").text();
		var itemNm		= $(G_grid01Rslt).find("ComCodes").find("ComCode").eq(j).find("itemNm").text();
		var restQty		= $(G_grid01Rslt).find("ComCodes").find("ComCode").eq(j).find("restQty").text();
		var itemSeq		= $(G_grid01Rslt).find("ComCodes").find("ComCode").eq(j).find("itemSeq").text();
		var flag				= $(G_grid01Rslt).find("ComCodes").find("ComCode").eq(j).find("flag").text();

		ji = j+1;
		
		var showFlag = "";
		var tagFirst = "";
		var tagLast = "";
		if(flag == "Y") showFlag = "유효";
		else showFlag = "삭제";
		
		if (flag == "N") { var bgColor = "#F2F2F2"; tagFirst = "<font color=\"#CCCCCC\">"; tagLast = "</font>"; }
		else var bgColor = "#FFFFFF";

		grid01Array[0] = ji;
		grid01Array[1] = tagFirst + groupCd + tagLast;
		grid01Array[2] = tagFirst + groupNm + tagLast;
		grid01Array[3] = tagFirst + itemCd + tagLast;
		grid01Array[4] = tagFirst + itemNm + tagLast;
		grid01Array[5] = tagFirst + restQty + tagLast;
		grid01Array[6] = tagFirst + itemSeq + tagLast;
		grid01Array[7] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,,,left,,", "fnSetComCode('"+groupCd+"','"+itemCd+"')", boldLine);
	}
}


//## 기초코드 상세정보 표시 ####################
function fnSetComCode(groupCd,itemCd)
{
	var valUrl = "/action/comCode/view.asp";
	var param = "groupCd="+groupCd;
	param += "&itemCd="+itemCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();			
			$("#groupCd").val($(xml).find("groupCd").text());
			$("#itemCd").val($(xml).find("itemCd").text());
			$("#itemNm").val($(xml).find("itemNm").text());
			$("#itemSeq").val($(xml).find("itemSeq").text());
			$("#flag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 기초 코드  정보 Reset 기능
function fnReset()
{
	$("#groupCd").val("");
	$("#itemCd").val("");
	$("#itemNm").val("");
	$("#itemSeq").val("");
	$("#flag").val("Y");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findItem").val("");
	$("#findComCdGroupCombo").val("");
}